package com.pig4cloud.pig.repair.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.OrderCodeUtil;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.repair.entity.PropertyRepairEntity;
import com.pig4cloud.pig.repair.feign.RemoteSysRoleService;
import com.pig4cloud.pig.repair.service.PropertyRepairService;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@RestController
@RequiredArgsConstructor
@RequestMapping("/propertyRepair" )
@Tag(description = "propertyRepair" , name = "报修表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PropertyRepairController {

    private final  PropertyRepairService propertyRepairService;
	@Autowired
	private RemoteSysRoleService remoteSysRoleService;
    @GetMapping("/page" )
    public R getPropertyRepairPage(@ParameterObject Page page, @ParameterObject PropertyRepairEntity propertyRepair) {
        LambdaQueryWrapper<PropertyRepairEntity> wrapper = Wrappers.<PropertyRepairEntity>lambdaQuery()
				.like(StrUtil.isNotBlank(propertyRepair.getRepairCode()),PropertyRepairEntity::getRepairCode,propertyRepair.getRepairCode())
				.like(StrUtil.isNotBlank(propertyRepair.getRequester()),PropertyRepairEntity::getRequester,propertyRepair.getRequester())
				.eq(StrUtil.isNotBlank(propertyRepair.getStatus()),PropertyRepairEntity::getStatus,propertyRepair.getStatus());

		return R.ok(propertyRepairService.page(page, wrapper));
    }
    @GetMapping("/{repairId}" )
    public R getById(@PathVariable("repairId" ) Long repairId) {
        return R.ok(propertyRepairService.getById(repairId));
    }
    @SysLog("新增报修表" )
    @PostMapping
    public R save(@RequestBody PropertyRepairEntity propertyRepair) {

		String repairCode = OrderCodeUtil.generateOrderCode("BX",4);
		propertyRepair.setRepairCode(repairCode);
        return R.ok(propertyRepairService.save(propertyRepair));
    }

    @SysLog("修改报修表" )
    @PutMapping
    public R updateById(@RequestBody PropertyRepairEntity propertyRepair) {
        return R.ok(propertyRepairService.updateById(propertyRepair));
    }
    @SysLog("通过id删除报修表" )
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(propertyRepairService.removeBatchByIds(CollUtil.toList(ids)));
    }

	@GetMapping("/roleList")
	public R getRoleList() {
		return remoteSysRoleService.listRoles();
	}

}
